﻿using DataBaseModel;
using KlubSportowy.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KlubSportowy.Controllers
{
    [Authorize]
    public class ClubMemberController : Controller
    {
        //
        // GET: /ClubMember/

        silownia_hadesEntities silownia = new silownia_hadesEntities();
        
        public ActionResult Index()
        {
            var members = silownia.uzytkownicy.Select(
                                                m => new ClubMemberViewModel()
                                                {
                                                    Id = m.id_Uzytkownicy,
                                                    UserName = m.Username,
                                                    Email = m.Email,
                                                    LastActivity = (DateTime)m.LastActivityDate,
                                                    LastLogOn = (DateTime)m.LastLoginDate,
                                                    RegisterDate = (DateTime)m.CreationDate
                                                }).ToList();
            
            return View(members);
        }

        //
        // GET: /ClubMember/Details/5

        public ActionResult Details(int id)
        {
            var user = silownia.uzytkownicy.SingleOrDefault(u => u.id_Uzytkownicy == id);

            var detailsUser = new ClubMemberViewModel()
                                    {
                                        Id = user.id_Uzytkownicy,
                                        UserName = user.Username,
                                        Email = user.Email,
                                        LastActivity = (DateTime)user.LastActivityDate,
                                        LastLogOn = (DateTime)user.LastLoginDate,
                                        RegisterDate = (DateTime)user.CreationDate
                                    };

              return View(detailsUser);
        }

        //
        // GET: /ClubMember/Edit/5

        public ActionResult Edit(int id)
        {
            var userEdit = silownia.uzytkownicy.SingleOrDefault(u => u.id_Uzytkownicy == id);
            var vieModel=  new ClubMemberViewModel() { Id= userEdit.id_Uzytkownicy, UserName= userEdit.Username, Email= userEdit.Email};
            return View(vieModel);
        }

        //
        // POST: /ClubMember/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var userEdit = silownia.uzytkownicy.SingleOrDefault(m => m.id_Uzytkownicy == id);

                string userName = collection["UserName"];
                string email = collection["Email"];

                userEdit.Username = userName;
                userEdit.Email = email;

                silownia.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /ClubMember/Delete/5

        public ActionResult Delete(int id)
        {
            var user = silownia.uzytkownicy.SingleOrDefault(m => m.id_Uzytkownicy == id);

            var userDelete = new ClubMemberViewModel()
            {
                UserName = user.Username,
                Email = user.Email
            };

            return View(userDelete);
        }

        //
        // POST: /ClubMember/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                var userDelete = silownia.uzytkownicy.SingleOrDefault(m => m.id_Uzytkownicy == id);
                silownia.uzytkownicy.Remove(userDelete);

                silownia.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
